//
//  SheQuViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "SheQuViewController.h"
#import "SheQuViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "DetailSheQuViewController.h"

@interface SheQuViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;


@property (nonatomic, strong) NSMutableArray *sectionArray;


@end

@implementation SheQuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // self.navigationController.navigationBar.backgroundColor = kMainColor;
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"社区";
    
//    [[TYDataManager shareManager] requestDataWithUrl:kSheQuAPI block:^(NSData *data){
//        
//     NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//        NSMutableDictionary *listDic = dataDic[@"data"];
//        NSArray *array = listDic[@"forum_list"];
//        for (NSDictionary *dic in array) {
//         SheQuSectionModel *sectionModel = [[SheQuSectionModel alloc] init];
//            [sectionModel setValuesForKeysWithDictionary:dic];
//            [self.sectionArray addObject:sectionModel];
//            
//           NSArray *groupArray = dic[@"group"];
//            // 新建数组 用来 装model
//            NSMutableArray *array = [NSMutableArray array];
//            for (NSDictionary *dict in groupArray) {
//                SheQuModel *model = [[SheQuModel alloc] init];
//                [model setValuesForKeysWithDictionary:dict];
//                [array addObject:model];
//                
//                sectionModel.groups = array;
//            }
//            
//            
//        }
//        // 刷新数据
//        [self.collectionView reloadData];
//    }];
//
//    [self addSubViews];
    
    [self setUpData];
    [self addSubViews];
    [self setUpRefresh];
    
}

// 加载数据
- (void)setUpData
{
    [[TYDataManager shareManager] requestDataWithUrl:kSheQuAPI block:^(NSData *data){
        
        if (!self.isRefresh) {
            [self.sectionArray removeAllObjects];
        }
        
        NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSMutableDictionary *listDic = dataDic[@"data"];
        NSArray *array = listDic[@"forum_list"];
        for (NSDictionary *dic in array) {
            SheQuSectionModel *sectionModel = [[SheQuSectionModel alloc] init];
            [sectionModel setValuesForKeysWithDictionary:dic];
            [self.sectionArray addObject:sectionModel];
            
            NSArray *groupArray = dic[@"group"];
            // 新建数组 用来 装model
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dict in groupArray) {
                SheQuModel *model = [[SheQuModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [array addObject:model];
                sectionModel.groups = array;
            }
            
            
        }
        // 刷新数据
        [self.collectionView reloadData];
    }];

}


// 懒加载方法

- (NSMutableArray *)sectionArray
{
    if (!_sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}


#pragma mark -- 刷新
- (void)setUpRefresh
{
    // 下拉刷新
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [self.collectionView.header beginRefreshing];
    
}

// 实现下拉刷新的方法
- (void)headerRereshing
{
    self.isRefresh = NO;
    [self setUpData];
    [self.collectionView.header endRefreshing];
}



// 创建集合视图
- (void)addSubViews
{
   // 创建网格布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 行间距
    layout.minimumLineSpacing = 10;
    // 列间距
    layout.minimumInteritemSpacing = 10;
    // item宽高
   layout.itemSize = CGSizeMake((kScreenWidth - 40) / 2, 115);

    // 滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 头
    layout.headerReferenceSize = CGSizeMake(0, 40);
    // 内边距
    layout.sectionInset = UIEdgeInsetsMake(5, 15, 5, 15);
    
    
    // 利用 layout创建一个集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
    // 注册表头
    [self.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SheQuHeader"];
    
    
    // 注册cell
    [self.collectionView registerClass:[SheQuViewCell class] forCellWithReuseIdentifier:@"SheQuCell"];
    
    
    
}


#pragma mark -- 代理方法
// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.sectionArray.count;
}

// 每个分区的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SheQuSectionModel *model = self.sectionArray[section];
    return model.groups.count;
   
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    SheQuViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SheQuCell" forIndexPath:indexPath];
   //cell.contentView.backgroundColor = [UIColor brownColor];
    if (self.sectionArray > 0) {
       
        SheQuSectionModel *sectionModel = self.sectionArray[indexPath.section];
        SheQuModel *model = sectionModel.groups[indexPath.row];
        
        [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:nil];
        cell.nameLable.text = model.name;
        cell.nameLable.font = [UIFont systemFontOfSize:13];
        cell.totalLable.text = [NSString stringWithFormat:@"%@个帖子", model.total_threads];
        cell.totalLable.font = [UIFont systemFontOfSize:13];
        
        
    }
     return cell;
    
}

// 返回表头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SheQuHeader" forIndexPath:indexPath];
   // headerView.backgroundColor = [UIColor purpleColor];
    SheQuSectionModel *sheQuModel = self.sectionArray[indexPath.section];
    headerView.sectionLable.text = sheQuModel.name;
    headerView.sectionLable.font = [UIFont systemFontOfSize:20];
    headerView.sectionLable.alpha = 0.7;
    return headerView;
}


// 被选中时的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailSheQuViewController *detailSQVC = [[DetailSheQuViewController alloc] init];
    SheQuSectionModel *sectionModel = self.sectionArray[indexPath.section];
    SheQuModel *model = sectionModel.groups[indexPath.row];
    detailSQVC.deSQModel = model;
    detailSQVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailSQVC animated:YES];
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
